chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    let url = new URL(details.url);
    if (!url.searchParams.has('skip')) {
      url.searchParams.set('skip', 'true');
    }
    return {redirectUrl: url.href};
  },
  {urls: ["*://localhost:*/*","*://127.0.0.1:*/*"]}, // 可以改为具体的 URL 模式
  ["blocking"]
);

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if (message.type === '修改') {
    addData(message.key,message.val)
    sendResponse({ response:message });
  }
  if(message.type === '删除'){
    deleteData(message.key);
    sendResponse({ response:message });
  }
});


function deleteData(key){
  chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
      let url = new URL(details.url);
      if (url.searchParams.has(key)) {
        url.searchParams.delete(key);
      }
      return {redirectUrl: url.href};
    },
    {urls: ["*://localhost:*/*","*://127.0.0.1:*/*"]}, // 可以改为具体的 URL 模式
    ["blocking"]
  );
}

function addData(key,value){
  chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
      let url = new URL(details.url);
      if(url.searchParams.has(key)){
        if(url.searchParams.get(key) === value){
          return ;
        }
      }
      url.searchParams.set(key,value);
      return  {redirectUrl: url.href};
    },
    {urls:  ["*://localhost:*/*","*://127.0.0.1:*/*"]}, // 可以改为具体的 URL 模式
    ["blocking"]
  );
}